function add(a, b) {
  return a + b;
}
// const obj = {}
// const p = new add(1,3);
// console.log(new in )

/*
1. 保存原函数​
2. 提取“部分参数”​
​3. 确定最终绑定的 this
4. 返回新函数​
5. 新函数内部：合并参数​
6. 调用原函数并返回结果​
7. （可选但重要）处理“new 调用”​
*/
Function.prototype.myBind = function (ctx, ...args) {
  ctx = ctx === null || ctx === undefined ? globalThis : Object(ctx);
  const originalFunc = this;
  const Fn =  function (...bindArgs) {
    const allArgs = [...args, ...bindArgs];
    console.log('20', this instanceof Fn)
    let res =
      new.target === Fn
        ? new originalFunc(...allArgs)
        : originalFunc.apply(ctx, allArgs);
    return res;
  };
//   Fn.prototype = Object.create(this.prototype);
  return Fn;
};

function Person(name, age) {
  this.name = name;
  this.age = age;
}

const bindFn = Person.myBind(123, 1, 999);
const res = new bindFn();
console.log("result", res);
